package com.aotain.oframework.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.aotain.oframework.util.lang.StringUtil;

/**
 * @author Jacob.cw.Zhang@gmail.com
 */
public class AmountUtil {

	public static String moneyFilter(String money) {
		String regEx = "[,$]";
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(money);
		return matcher.replaceAll("").trim();
	}
	
	public static String getDisplayMoneyFormat(String money) {
		if (StringUtil.isEmptyString(money)) {
			return "0.00";
		}
		DecimalFormat decimalFormat = new DecimalFormat("#,###,##0.00");
		String returnStr = "0.00";
		double d;
		try {
			d = Double.parseDouble(money);
			returnStr = decimalFormat.format(d);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnStr;
	}
	
	public static String getDisplayMoneyFormat(BigDecimal money) {
		if (money == null) {
			return "0.00";
		}
		DecimalFormat decimalFormat = new DecimalFormat("#,###,##0.00");
		String returnStr = "0.00";
		try {
			returnStr = decimalFormat.format(money.doubleValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnStr;
	}
	
	public static String getDisplayDoubleDigitFormat(BigDecimal amount) {
		if (amount == null) {
			return "0";
		}
		DecimalFormat decimalFormat = new DecimalFormat("#0");
		String returnStr = "0";
		try {
			returnStr = decimalFormat.format(amount.doubleValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnStr;
	}
	
	public static String getDisplayDoubleDigitFormat(String amount) {
		if (StringUtil.isEmptyString(amount)) {
			return "0";
		}
		DecimalFormat decimalFormat = new DecimalFormat("#0");
		String returnStr = "0";
		try {
			returnStr = decimalFormat.format(Double.parseDouble(amount));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnStr;
	}
	
	public static String getDisplayThreeTimeDigitFormat(BigDecimal amount){
		if(null == amount){
			return "0";
		}
        DecimalFormat fmt = new DecimalFormat("##0"); 
        String outStr = "0";  
        try {  
            outStr = fmt.format(amount.doubleValue());  
        } catch (Exception e) {  
        	e.printStackTrace();
        }  
        return outStr;  
    }
	
	public static String getDisplayThreeTimeDigitFormat(String amount){
		if(StringUtil.isEmptyString(amount)){
			return "0";
		}
        DecimalFormat fmt = new DecimalFormat("##0"); 
        String outStr = "0"; 
        try {  
            outStr = fmt.format(Double.parseDouble(amount));  
        } catch (Exception e) {  
        	e.printStackTrace();
        }  
        return outStr;  
    }
	
	public static Double getMoneyDouble(String money){
		money = moneyFilter(money);
		if(AmountUtil.isValid(money)){
			return Double.valueOf(money);
		}
		return new Double(0.00);
	}
	
	public static BigDecimal getMoneyBigDecimal(String money){
		money = moneyFilter(money);
		if(AmountUtil.isValid(money)){
			return BigDecimal.valueOf(Double.valueOf(money));
		}
		return new BigDecimal(0.00);
	}
	
	public static boolean isValid(String money){
		String regex = "^(-?\\d+)(\\.\\d{0,2})?$";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(money);
		if(matcher.matches()){
			return true;
		}else{
			return false;
		}
	}
	
	public static void main(String[] args) {
		String money = "";
		System.out.println(money);
		System.out.println(AmountUtil.getDisplayMoneyFormat(money));
		System.out.println(StringUtil.isAllDigit(money));
		System.out.println(isValid(money));
		BigDecimal overPayment = new BigDecimal("5000");
		System.out.println(AmountUtil.getDisplayMoneyFormat(overPayment));
	}
	
}
